﻿using System;
using System.Windows.Forms;

namespace Random_Password_Generator
{
    public partial class Form1 : Form
    {

        #region Variables

        private int characterCount = 6;
        private bool useNumbers = true;
        private bool useLowercase = true;
        private bool useUppercase = false;
        private bool useSymbols = false;
        private string password = "";
        private Random random = new Random();
        private int passwordCharacterCount = 0;
        private int complexityCoefficient = 1;

        #endregion

        #region Contructor

        public Form1()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Functions

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label6.Text = "";
            comboBox1.Text = "6";
            for (int i = 6; i < 33; i++)
            {
                comboBox1.Items.Add(i);
            }
            complexityCoefficient = trackBar1.Value;

        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            label6.Text = "";
            password = "";
            int xCounter = 1;
            bool firstPass = true;
            int rnd02 = random.Next(1, 100);
            do
            {
                passwordCharacterCount = Convert.ToInt32(comboBox1.Text);
                if (firstPass == true)
                {
                    if (useLowercase == true)
                    {
                        password = password + (char)random.Next(97, 122);
                        xCounter += 1;
                        firstPass = false;
                    }
                }
                if (firstPass == true)
                {
                    if (useNumbers == true)
                    {
                        password = password + (char)random.Next(48, 57);
                        xCounter += 1;
                        firstPass = false;
                    }
                }
                if (firstPass == true)
                {
                    if (useUppercase == true)
                    {
                        password = password + (char)random.Next(65, 90);
                        xCounter += 1;
                        firstPass = false;
                    }
                }
                if (firstPass == true)
                {
                    if (useSymbols == true)
                    {
                        int rnd05 = random.Next(1, 4);
                        if (rnd05 == 1)
                        {
                            password = password + (char)random.Next(33, 47);
                            xCounter += 1;
                            firstPass = false;
                        }
                        if (rnd05 == 2)
                        {
                            password = password + (char)random.Next(58, 64);
                            xCounter += 1;
                            firstPass = false;
                        }
                        if (rnd05 == 3)
                        {
                            password = password + (char)random.Next(91, 96);
                            xCounter += 1;
                            firstPass = false;
                        }
                        if (rnd05 == 4)
                        {
                            password = password + (char)random.Next(123, 126);
                            xCounter += 1;
                            firstPass = false;
                        }
                    }
                }

                rnd02 = random.Next(1, 100);

                switch (complexityCoefficient)
                {
                    case 1://Least complex, password weighted 25% numberws, 60% lowercase and 15% symbols and uppercase

                        if (rnd02 > 0 && rnd02 < 25)
                        {
                            if (useNumbers == true)
                            {
                                password = password + (char)random.Next(48, 57);
                                xCounter += 1;
                            }
                        }
                        if (rnd02 > 24 && rnd02 < 85)
                        {
                            if (useLowercase == true)
                            {
                                password = password + (char)random.Next(97, 122);
                                xCounter += 1;
                            }
                        }
                        if (rnd02 > 84 && rnd02 < 101)
                        {
                            if (useUppercase == true)
                            {
                                int rnd03 = random.Next(1, 10);
                                if (rnd03 > 8)
                                {
                                    password = password + (char)random.Next(65, 90);
                                    xCounter += 1;
                                }
                            }
                            if (useSymbols == true)
                            {
                                int rnd04 = random.Next(1, 10);
                                if (rnd04 > 8)
                                {
                                    int rnd05 = random.Next(1, 4);
                                    if (rnd05 == 1)
                                    {
                                        password = password + (char)random.Next(33, 47);
                                        xCounter += 1;
                                    }
                                    if (rnd05 == 2)
                                    {
                                        password = password + (char)random.Next(58, 64);
                                        xCounter += 1;
                                    }
                                    if (rnd05 == 3)
                                    {
                                        password = password + (char)random.Next(91, 96);
                                        xCounter += 1;
                                    }
                                    if (rnd05 == 4)
                                    {
                                        password = password + (char)random.Next(123, 126);
                                        xCounter += 1;
                                    }
                                }
                            }
                        }
                        break;

                    case 2: //Slightly complex, password weighted 55% lowercase, 25% numbers and 20% symbols and uppercase

                        int rnd06 = random.Next(1, 100);
                        if (rnd06 > 0 && rnd06 < 25)
                        {
                            if (useNumbers == true)
                            {
                                password = password + (char)random.Next(48, 57);
                                xCounter += 1;
                            }
                        }
                        if (rnd06 > 24 && rnd06 < 80)
                        {
                            if (useLowercase == true)
                            {
                                password = password + (char)random.Next(97, 122);
                                xCounter += 1;
                            }
                        }
                        if (rnd06 > 79 && rnd06 < 101)
                        {
                            if (useUppercase == true)
                            {
                                int rnd07 = random.Next(1, 10);
                                if (rnd06 > 8)
                                {
                                    password = password + (char)random.Next(65, 90);
                                    xCounter += 1;
                                }
                            }
                            if (useSymbols == true)
                            {
                                int rnd08 = random.Next(1, 10);
                                if (rnd08 > 8)
                                {
                                    int rnd09 = random.Next(1, 4);
                                    if (rnd09 == 1)
                                    {
                                        password = password + (char)random.Next(33, 47);
                                        xCounter += 1;
                                    }
                                    if (rnd09 == 2)
                                    {
                                        password = password + (char)random.Next(58, 64);
                                        xCounter += 1;
                                    }
                                    if (rnd09 == 3)
                                    {
                                        password = password + (char)random.Next(91, 96);
                                        xCounter += 1;
                                    }
                                    if (rnd09 == 4)
                                    {
                                        password = password + (char)random.Next(123, 126);
                                        xCounter += 1;
                                    }
                                }
                            }
                        }
                        break;

                    case 3:  //Complex, password weighted 50% lowercase, 25% numbers and 25% symbols and uppercase

                        int rnd10 = random.Next(1, 100);
                        if (rnd10 > 0 && rnd10 < 25)
                        {
                            if (useNumbers == true)
                            {
                                password = password + (char)random.Next(48, 57);
                                xCounter += 1;
                            }
                        }
                        if (rnd10 > 24 && rnd10 < 75)
                        {
                            if (useLowercase == true)
                            {
                                password = password + (char)random.Next(97, 122);
                                xCounter += 1;
                            }
                        }
                        if (rnd10 > 74 && rnd10 < 101)
                        {
                            if (useUppercase == true)
                            {
                                int rnd03 = random.Next(1, 10);
                                if (rnd03 > 5)
                                {
                                    password = password + (char)random.Next(65, 90);
                                    xCounter += 1;
                                }
                            }
                            if (useSymbols == true)
                            {
                                int rnd04 = random.Next(1, 10);
                                if (rnd04 > 5)
                                {
                                    int rnd05 = random.Next(1, 4);
                                    if (rnd05 == 1)
                                    {
                                        password = password + (char)random.Next(33, 47);
                                        xCounter += 1;
                                    }
                                    if (rnd05 == 2)
                                    {
                                        password = password + (char)random.Next(58, 64);
                                        xCounter += 1;
                                    }
                                    if (rnd05 == 3)
                                    {
                                        password = password + (char)random.Next(91, 96);
                                        xCounter += 1;
                                    }
                                    if (rnd05 == 4)
                                    {
                                        password = password + (char)random.Next(123, 126);
                                        xCounter += 1;
                                    }
                                }
                            }
                        }
                        break;

                    case 4://Complex, password weighted 34% lowercase, 33% numbers and 33% symbols and uppercase

                        int rnd11 = random.Next(1, 100);
                        if (rnd11 > 0 && rnd11 < 34)
                        {
                            if (useNumbers == true)
                            {
                                password = password + (char)random.Next(48, 57);
                                xCounter += 1;
                            }
                        }
                        if (rnd11 > 34 && rnd11 < 67)
                        {
                            if (useLowercase == true)
                            {
                                password = password + (char)random.Next(97, 122);
                                xCounter += 1;
                            }
                        }
                        if (rnd11 > 67 && rnd11 < 101)
                        {
                            if (useUppercase == true)
                            {
                                int rnd03 = random.Next(1, 10);
                                if (rnd03 > 2)
                                {
                                    password = password + (char)random.Next(65, 90);
                                    xCounter += 1;
                                }
                            }
                            if (useSymbols == true)
                            {
                                int rnd04 = random.Next(1, 10);
                                if (rnd04 > 2)
                                {
                                    int rnd05 = random.Next(1, 4);
                                    if (rnd05 == 1)
                                    {
                                        password = password + (char)random.Next(33, 47);
                                        xCounter += 1;
                                    }
                                    if (rnd05 == 2)
                                    {
                                        password = password + (char)random.Next(58, 64);
                                        xCounter += 1;
                                    }
                                    if (rnd05 == 3)
                                    {
                                        password = password + (char)random.Next(91, 96);
                                        xCounter += 1;
                                    }
                                    if (rnd05 == 4)
                                    {
                                        password = password + (char)random.Next(123, 126);
                                        xCounter += 1;
                                    }
                                }
                            }
                        }
                        break;
                }
                if (xCounter == 1)
                {
                    xCounter = 100;
                }
            }

            while (xCounter < passwordCharacterCount + 1);

            label6.Text = password;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            switch (checkBox1.CheckState)
            {
                case CheckState.Checked:
                    useNumbers = true;
                    break;
                case CheckState.Unchecked:
                    useNumbers = false;
                    break;

            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            switch (checkBox4.CheckState)
            {
                case CheckState.Checked:
                    useUppercase = true;
                    break;
                case CheckState.Unchecked:
                    useUppercase = false;
                    break;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            switch (checkBox2.CheckState)
            {
                case CheckState.Checked:
                    useLowercase = true;
                    break;
                case CheckState.Unchecked:
                    useLowercase = false;
                    break;
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            switch (checkBox3.CheckState)
            {
                case CheckState.Checked:
                    useSymbols = true;
                    break;
                case CheckState.Unchecked:
                    useSymbols = false;
                    break;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            characterCount = Convert.ToInt32(comboBox1.Text);
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            complexityCoefficient = trackBar1.Value;
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(label6.Text);
        }

        private void label5_Click(object sender, EventArgs e)
        {
            switch (checkBox1.CheckState)
            {
                case CheckState.Checked:
                    checkBox1.CheckState = CheckState.Unchecked;
                    useNumbers = false;
                    break;
                case CheckState.Unchecked:
                    checkBox1.CheckState = CheckState.Checked;
                    useNumbers = true;
                    break;

            }
        }

        private void label2_Click(object sender, EventArgs e)
        {
            switch (checkBox2.CheckState)
            {
                case CheckState.Checked:
                    checkBox2.CheckState = CheckState.Unchecked;
                    useLowercase = false;
                    break;
                case CheckState.Unchecked:
                    checkBox2.CheckState = CheckState.Checked;
                    useLowercase = true;
                    break;

            }
        }

        private void label3_Click(object sender, EventArgs e)
        {
            switch (checkBox4.CheckState)
            {
                case CheckState.Checked:
                    checkBox4.CheckState = CheckState.Unchecked;
                    useUppercase = false;
                    break;
                case CheckState.Unchecked:
                    checkBox4.CheckState = CheckState.Checked;
                    useUppercase = true;
                    break;

            }
        }

        private void label4_Click(object sender, EventArgs e)
        {
            switch (checkBox3.CheckState)
            {
                case CheckState.Checked:
                    checkBox3.CheckState = CheckState.Unchecked;
                    useSymbols = false;
                    break;
                case CheckState.Unchecked:
                    checkBox3.CheckState = CheckState.Checked;
                    useSymbols = true;
                    break;

            }
        }

        #endregion

    }
}
